package com.carlife.ui;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

import com.carlife.R;
import com.carlife.model.MileageLog;
import com.carlife.model.MyCar;
import com.carlife.provider.Conditions;
import com.carlife.provider.IModel;
import com.carlife.provider.MileageLogMgr;
import com.carlife.ui.widget.DigitalBar;
import com.carlife.util.Constants;
import com.carlife.util.DateUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MileageInput extends Activity implements View.OnClickListener {
	
	private DigitalBar mileageBar = null;
	
	static void show(Context context) {
        final Intent intent = new Intent(context, MileageInput.class);
        context.startActivity(intent);
    }

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mileage_input);
        setupViews();        
    }
    
    
    protected void setupViews() {
    	mileageBar = (DigitalBar)findViewById(R.id.db_mileage);
    	mileageBar.setValue(new BigDecimal(5000));  // Default value
    	mileageBar.setInterval(new BigDecimal(5));
    	mileageBar.setUnitString(getString(R.string.unit_milleage));
    	
    	Button btnOk = (Button)findViewById(R.id.mi_ok);
    	btnOk.setOnClickListener(this);
    	
    	Button btnCancel = (Button)findViewById(R.id.mi_ok);
    	btnCancel.setOnClickListener(this);
    	
    	showMileageTable();    	
    }
    
    /**
     * 根据加油记录显示table
     */
    private void showMileageTable() {    	
    	TableLayout table = (TableLayout)findViewById(R.id.mi_table_mileage);
    	MileageLogMgr mgr = new MileageLogMgr(this);
    	MileageLog model = new MileageLog();
    	Conditions cond = new Conditions();
    	cond.addOrder(MileageLog.FD_INPUT_TIME, false);
    	List<IModel> modelList = mgr.query(model, cond);
    	
    	Iterator iterator = modelList.iterator();
    	while(iterator.hasNext()) {
			MileageLog log = (MileageLog) iterator.next();
			table.addView(concreatTableRow(log));
		}
    }
    
    /**
     * 根据一个加油记录显示table中的一行
     */
    private TableRow concreatTableRow(MileageLog log) {
    	Log.d(Constants.LOG_TAG, log.toString());
    	TableRow row = new TableRow(this);
    	
    	// 添加日期
    	TextView dataTxt = new TextView(this);
    	dataTxt.setId(1);    	
    	dataTxt.setPadding(5, 5, 5, 5);
    	dataTxt.setText(log.get(log.FD_INPUT_TIME).toString());
    	row.addView(dataTxt);
    	
    	// 添加加油数值
    	TextView mileageTxt = new TextView(this);
    	mileageTxt.setId(2);    	
    	mileageTxt.setPadding(5, 5, 5, 5);
    	mileageTxt.setText(log.get(log.FD_MILEAGE_VAlUE).toString());
    	row.addView(mileageTxt);
    	
    	// 编辑图标
    	ImageView removeSymbolImg = new ImageView(this);
    	removeSymbolImg.setPadding(5, 5, 5, 5);
    	removeSymbolImg.setImageResource(R.drawable.close_button);
    	removeSymbolImg.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View view) { 
	            	// TODO
	            }           
	        });
		row.addView(removeSymbolImg);
    	
    	return row;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.oillife, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_home:
                Welcome.show(this);
                return true;
            case R.id.menu_item_oil_input:
                OilInput.show(this);
                return true;
            case R.id.menu_item_mileage_input:
            	// MileageInput.show(this);
                return true;
            case R.id.menu_item_statics:
            	MileageLineChart.show(this);
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }
    
    
    /**
     * 响应该节点界面上点击事件
     */
    @Override
	public void onClick(View clickFocus) {
    	switch (clickFocus.getId()) {
		case R.id.mi_ok:   // 确认按钮的响应
			MileageLogMgr mgr = new MileageLogMgr(this);
	    	MileageLog model = new MileageLog();
	    	model.put(MileageLog.FD_CAR_NO, MyCar.DUMMY_NO);
	    	model.put(MileageLog.FD_INPUT_TIME, DateUtil.currentTimeFormatted());
	    	model.put(MileageLog.FD_MILEAGE_VAlUE, mileageBar.getValue().toPlainString());
	    	
	    	mgr.insert(model);
	    	
	    	//this.mileageBar.setValue(new BigDecimal(0));
	    	//showMileageTable();
	    	MileageInput.show(this);
	    	
			break;
		case R.id.mi_cancel: // 取消按钮的响应
			// 现在还没有实现
			break;
		default:
			break;
		}
	}


}
